package sk.fiit.lulco.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
//import javax.swing.JDialog;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SettingsWindow extends JFrame {

	private static final long serialVersionUID = 518563109075068075L;
	private Settings settings;
	private JTextField generationsCount;
	private JTextField individualsInGeneration;
	private JTextField eliteCount;
	private JTextField newGenerationIndividuals;
	private JTextField timeToLive;
	private JTextField maxPreviousColorsSize;
	private JTextField conflictedVerticesMutationProbability;
	private JTextField nonConflictedVerticesMutationProbability;

	public SettingsWindow(JFrame frame, Settings settings) {
		super("Settings");
		this.settings = settings;
		setLocation(300, 200);
		setSize(new Dimension(350, 300));
		setVisible(true);
		addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				super.windowClosing(e);
				cancelPerformed();
			}
		});
		initLayout();
//		pack();
	}

	private void initLayout() {
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(3, 3, 3, 3);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.1;
		c.gridx = 0;
		c.gridy = 0;
		c.weighty = 1;

		JLabel label = new JLabel("Generations count");
		add(label, c);
		c.gridx++;
		generationsCount = new JTextField();
		generationsCount.setHorizontalAlignment(JTextField.RIGHT);
		generationsCount.setMinimumSize(new Dimension(50, 10));
		generationsCount.setText(Integer.toString(Settings.getGenerationsCount()));
		add(generationsCount, c);

		c.gridx = 0;
		c.gridy++;
		//c.weightx = 0.1;
		label = new JLabel("Individuals in generation");
		add(label, c);
		c.gridx++;
		//c.weightx = 0.9;
		individualsInGeneration = new JTextField();
		individualsInGeneration.setHorizontalAlignment(JTextField.RIGHT);
		individualsInGeneration.setText(Integer.toString(Settings.getIndividualsInGeneration()));
		add(individualsInGeneration, c);

		c.gridx = 0;
		c.gridy++;
		//c.weightx = 0.1;
		label = new JLabel("Elite count");
		add(label, c);
		c.gridx++;
		//c.weightx = 0.9;
		eliteCount = new JTextField();
		eliteCount.setHorizontalAlignment(JTextField.RIGHT);
		eliteCount.setText(Integer.toString(Settings.getEliteCount()));
		add(eliteCount, c);

		c.gridx = 0;
		c.gridy++;
		//c.weightx = 0.1;
		label = new JLabel("New generation individuals");
		add(label, c);
		c.gridx++;
		//c.weightx = 0.9;
		newGenerationIndividuals = new JTextField();
		newGenerationIndividuals.setHorizontalAlignment(JTextField.RIGHT);
		newGenerationIndividuals.setText(Integer.toString(Settings.getNewGenerationIndividuals()));
		add(newGenerationIndividuals, c);

		c.gridx = 0;
		c.gridy++;
		//c.weightx = 0.1;
		label = new JLabel("Time to live");
		add(label, c);
		c.gridx++;
		//c.weightx = 0.9;
		timeToLive = new JTextField();
		timeToLive.setHorizontalAlignment(JTextField.RIGHT);
		timeToLive.setText(Integer.toString(Settings.getTimeToLive()));
		add(timeToLive, c);

		c.gridx = 0;
		c.gridy++;
		//c.weightx = 0.1;
		label = new JLabel("Max previous colors size");
		add(label, c);
		c.gridx++;
		//c.weightx = 0.9;
		maxPreviousColorsSize = new JTextField();
		maxPreviousColorsSize.setHorizontalAlignment(JTextField.RIGHT);
		maxPreviousColorsSize.setText(Integer.toString(Settings.getMaxPreviousColorsSize()));
		add(maxPreviousColorsSize, c);

		c.gridx = 0;
		c.gridy++;
		//c.weightx = 0.1;
		label = new JLabel("Conflicted vertices mutation probability");
		add(label, c);
		c.gridx++;
		//c.weightx = 0.9;
		conflictedVerticesMutationProbability = new JTextField();
		conflictedVerticesMutationProbability.setHorizontalAlignment(JTextField.RIGHT);
		conflictedVerticesMutationProbability.setText(Double.toString(Settings
				.getConflictedVerticesMutationProbability()));
		add(conflictedVerticesMutationProbability, c);

		c.gridx = 0;
		c.gridy++;
		//c.weightx = 0.1;
		label = new JLabel("Non conflicted vertices mutation probability");
		add(label, c);
		c.gridx++;
		//c.weightx = 0.9;
		nonConflictedVerticesMutationProbability = new JTextField();
		nonConflictedVerticesMutationProbability.setHorizontalAlignment(JTextField.RIGHT);
		nonConflictedVerticesMutationProbability.setText(Double.toString(Settings
				.getNonConflictedVerticesMutationProbability()));
		add(nonConflictedVerticesMutationProbability, c);

		c.gridx = 0;
		c.gridy++;
		c.gridwidth = 2;
		c.weightx = 1;
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		add(panel, c);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				cancelPerformed();
			}
		});
		panel.add(cancelButton);

		JButton saveButton = new JButton("Save");
		saveButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				savePerformed();
			}
		});
		panel.add(saveButton);

		JButton saveAndCloseButton = new JButton("Save and close");
		saveAndCloseButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				saveAndClosePerformed();
			}
		});
		panel.add(saveAndCloseButton);
	}

	private void cancelPerformed() {
		dispose();
	}

	private void saveAndClosePerformed() {
		savePerformed();
		dispose();
	}

	private void savePerformed() {
		if (!generationsCount.getText().equals("")) {
			try {
				Settings.setGenerationsCount(Integer.parseInt(generationsCount.getText()));
			} catch (NumberFormatException e) {

			}
		}
		if (!individualsInGeneration.getText().equals("")) {
			try {
				Settings.setIndividualsInGeneration(Integer.parseInt(individualsInGeneration.getText()));
			} catch (NumberFormatException e) {

			}
		}
		if (!eliteCount.getText().equals("")) {
			try {
				Settings.setEliteCount(Integer.parseInt(eliteCount.getText()));
			} catch (NumberFormatException e) {

			}
		}
		if (!newGenerationIndividuals.getText().equals("")) {
			try {
				Settings.setNewGenerationIndividuals(Integer.parseInt(newGenerationIndividuals.getText()));
			} catch (NumberFormatException e) {

			}
		}
		if (!timeToLive.getText().equals("")) {
			try {
				Settings.setTimeToLive(Integer.parseInt(timeToLive.getText()));
			} catch (NumberFormatException e) {

			}
		}
		if (!maxPreviousColorsSize.getText().equals("")) {
			try {
				Settings.setMaxPreviousColorsSize(Integer.parseInt(maxPreviousColorsSize.getText()));
			} catch (NumberFormatException e) {

			}
		}
		if (!conflictedVerticesMutationProbability.getText().equals("")) {
			try {
				Settings.setConflictedVerticesMutationProbability(Double
						.parseDouble(conflictedVerticesMutationProbability.getText()));
			} catch (NumberFormatException e) {

			}
		}
		if (!nonConflictedVerticesMutationProbability.getText().equals("")) {
			try {
				Settings.setNonConflictedVerticesMutationProbability(Double
						.parseDouble(nonConflictedVerticesMutationProbability.getText()));
			} catch (NumberFormatException e) {

			}
		}
	}

	public Settings getSettings() {
		return settings;
	}
}
